package net.hn.hnms.biz.api.feign.base;

import com.huaweicloud.common.context.InvocationContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.core.Ordered;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

/**
 * 智呈物联 pansx
 * 2021/10/30 16:04
 * 1.0 version
 */
@Configuration
public class FeignClientsConfigurationCustom implements RequestInterceptor, Ordered {
    public static String [] uris={"/expert/import/","/expert/import","/rescueTeam/import","/rescueTeam/import/",
            "/material/import","/material/import/"};//需要过滤的url调用
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));
    }


    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));//为空进行新建
            attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//重新赋值
            //线程上下文信息
            InvocationContextHolder.deserializeAndCreate("{}");
        }


        HttpServletRequest request = attributes.getRequest();
        String requestURI = request.getRequestURI();
        List<String> list= Arrays.asList(uris);
      if(list.contains(requestURI)) return;
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                Enumeration<String> values = request.getHeaders(name);
                if (name.equals("content-length")){
                    continue;
                }
                while (values.hasMoreElements()) {
                    String value = values.nextElement();

                    requestTemplate.header(name, value);
                    System.out.println(name + "---------------" + value);
                }
            }
        }
    }

    @Override
    public int getOrder() {
        return -1;
    }
}